package huriezy.quickcontacts.activities.fragments;

import huriezy.quickcontacts.R;
import huriezy.quickcontacts.models.ContactController;
import huriezy.quickcontacts.models.LogDebug;
import huriezy.quickcontacts.models.interfaces.IContactManager;
import huriezy.quickcontacts.models.managers.ContactPhoneCallListener;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NameAndPhoneFragment extends ContactFragment {

	private boolean						activated			= false;
	private ContactPhoneCallListener	phoneCallManager	= null;

	public static NameAndPhoneFragment newInstance(String name, String number) {
		NameAndPhoneFragment instance = new NameAndPhoneFragment();
		Bundle args = new Bundle();
		args.putString(IContactManager.NAME_TAG, name);
		args.putString(IContactManager.NUMBER_TAG, number);
		instance.setArguments(args);
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View result = inflater.inflate(R.layout.fragment_name_and_phone, container, false);
		Bundle args = getArguments();
		if (args == null) {
			args = savedInstanceState;
			LogDebug.info(" * Use savedInstanceState for " + getClass().getSimpleName(), getClass());
		}
		else {
			LogDebug.info(" * Use newInstance bundle for " + getClass().getSimpleName(), getClass());
		}
		if (args != null) {
			String name = args.getString(IContactManager.NAME_TAG);
			setName(name, result);
			String number = args.getString(IContactManager.NUMBER_TAG);
			setNumber(number, result);
		}
		else {
			LogDebug.warn("No saved informations.", getClass());
		}

		initActions(result);

		return result;
	}

	@Override
	public void clearContactData() {
		setName("", getView());
		setNumber("", getView());
	}

	@Override
	public void saveContactData() {
		IContactManager contactManager = ContactController.getInstance().getContactManager(getActivity());
		contactManager.putContactData(IContactManager.NAME_TAG, getName(getView()));
		contactManager.putContactData(IContactManager.NUMBER_TAG, getNumber(getView()));
	}

	/**
	 * Init actions for button inside this {@link Fragment}.
	 */
	private void initActions(View parentView) {
		LogDebug.info("Adding actions", getClass());
		final EditText phoneEditText = (EditText) parentView.findViewById(R.id.contact_phone_editText);
		final Context parentContext = getActivity();

		Button waitIncomingCallButton = (Button) parentView.findViewById(R.id.wait_incoming_call_button);
		waitIncomingCallButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TelephonyManager telephonyManager = (TelephonyManager) parentContext
						.getSystemService(Context.TELEPHONY_SERVICE);
				TextView inWaitMessage = (TextView) getActivity().findViewById(R.id.in_wait_textView);
				if (activated) {
					LogDebug.info("Setting OFF phoneCallListener", parentContext.getClass() );
					((Button) v).setTextColor(Color.BLACK);
					if(inWaitMessage != null){
						inWaitMessage.setVisibility(View.GONE);
					}
					if (phoneCallManager != null) {
						telephonyManager.listen(phoneCallManager, PhoneStateListener.LISTEN_NONE);
						activated = !activated;
					}
				}
				else {
					LogDebug.info("Setting ON phoneCallListener", parentContext.getClass());
					((Button) v).setTextColor(Color.RED);
					if(inWaitMessage != null){
						inWaitMessage.setVisibility(View.VISIBLE);
					}
					if (phoneCallManager == null) {
						phoneCallManager = new ContactPhoneCallListener(phoneEditText);
					}
					telephonyManager.listen(phoneCallManager, PhoneStateListener.LISTEN_CALL_STATE);
					activated = !activated;
				}
			}
		});

		Button lastCallButton = (Button) parentView.findViewById(R.id.last_call_button);
		lastCallButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (phoneEditText != null) {
					String lastNumber = ContactPhoneCallListener.getLastCall(getActivity());
					if (lastNumber != null) {
						phoneEditText.setText(lastNumber);
					}
				}
			}
		});
	}

	public void setName(String name, View view) {
		EditText edittext = getNameEditText(view);
		if (edittext != null) {
			edittext.setText(name);
		}
	}

	public void setNumber(String number, View view) {
		EditText edittext = getPhoneEditText(view);
		if (edittext != null) {
			edittext.setText(number);
		}
	}

	public String getName(View view) {
		String result = "";
		EditText edittext = getNameEditText(view);
		if (edittext != null) {
			result = edittext.getText().toString();
		}
		return result;
	}

	public String getNumber(View view) {
		String result = "";
		EditText edittext = getPhoneEditText(view);
		if (edittext != null) {
			result = edittext.getText().toString();
		}
		return result;
	}

	private EditText getNameEditText(View view) {
		if (view != null) {
			return (EditText) view.findViewById(R.id.contact_name_editText);
		}
		else {
			LogDebug.warn("No Edit text view founded for contact name.", getClass());
		}
		return null;
	}

	private EditText getPhoneEditText(View view) {
		if (view != null) {
			return (EditText) view.findViewById(R.id.contact_phone_editText);
		}
		else {
			LogDebug.warn("No Edit text view founded for contact phone number.", getClass());
		}
		return null;
	}

}
